package com.wei.frame.db;

import android.content.Context;

import androidx.room.Room;
import androidx.room.RoomDatabase;

/**
 * 作者:赵若位
 * 时间:2020/12/22 16:21
 * 功能:数据库客户端
 */
//@Database(entities = {}, version = 1, exportSchema = true)
public abstract class DBManager extends RoomDatabase {

    private static volatile DBManager sManager = null;

    public static DBManager getInstance(Context context) {
        if (null == sManager) {
            synchronized (DBManager.class) {
                if (null == sManager) {
                    sManager = Room.databaseBuilder(context,
                            DBManager.class,
                            context.getPackageName() + ".db")
                            .fallbackToDestructiveMigration()//数据库升级的时候不会崩溃，但是数据会重置,数据会清空
//                            .addMigrations()
//                            .allowMainThreadQueries()//是否允许在主线程操作数据库
//                            .createFromAsset()
                            .build();
                }
            }
        }
        return sManager;
    }

//    public static final Migration MIGRATION_1_2 = new Migration(1, 2) {
//        @Override
//        public void migrate(@NonNull SupportSQLiteDatabase database) {
//            //首先创建一个临时表temp_newbean
//            database.execSQL("CREATE TABLE temp_newbean (" +
//                    "uniquekey text PRIMARY KEY NOT NULL," +
//                    "title text," +
//                    "date INTEGER NOT NULL DEFAULT 99," +
//                    "category text," +
//                    "author_name text," +
//                    "url text," +
//                    "thumbnail_pic_s text," +
//                    "thumbnail_pic_s02 text," +
//                    "thumbnail_pic_s03 text," +
//                    "is_content text" +
//                    ")");
//            //从newbean表中把数据插入到临时表中
//            database.execSQL("INSERT INTO temp_newbean (uniquekey,title,date,category,author_name,url,thumbnail_pic_s,thumbnail_pic_s02,thumbnail_pic_s03)" +
//                    "SELECT uniquekey,title,date,category,author_name,url,thumbnail_pic_s,thumbnail_pic_s02,thumbnail_pic_s03 FROM newbean");
//            //删除表newbean
//            database.execSQL("DROP TABLE newbean");
//            //将临时表名字修改为newbean
//            database.execSQL("ALTER TABLE temp_newbean RENAME TO newbean");
//        }
//    };

//    public abstract NewsDao mNewsDao();

}
